package com.shycloud.mido.homework.pattern;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.apache.commons.collections4.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shycloud.mido.common.api.constants.HomeWorkConstants;
import com.shycloud.mido.common.api.enums.HomeworkStatusEnum;
import com.shycloud.mido.common.core.util.SpringContextHolder;
import com.shycloud.mido.homework.api.entity.MultiTeacherOfflineClass;
import com.shycloud.mido.homework.api.vo.HomeworkDashboardRespVO;
import com.shycloud.mido.homework.mapper.MidoHomeworkMainMapper;
import com.shycloud.mido.homework.mapper.MultiTeacherOfflineClassMapper;
import com.shycloud.mido.homework.mapper.OnlineCourseInfoMapper;
import com.shycloud.mido.homework.mapper.OnlineCoursePeriodMapper;
import com.shycloud.mido.member.api.entity.OnlineCourseInfo;
import com.shycloud.mido.member.api.entity.OnlineCoursePeriod;


/**
 * @author baiqiang.
 * @Description: TODO
 * @date 2021年7月22日 下午2:56:41
 * @ClassName: BaseHomeworkDatas
 */
public class BaseHomeworkDatas {


	public void getAllDatas(LocalDate startDate, HomeworkDashboardRespVO mapBean, String studentId, List<String> teacherIds) {

		this.getHomeworkDatas(startDate, mapBean, studentId, teacherIds);
		this.getOfflineClassDatas(startDate, mapBean, studentId, teacherIds);
		this.getOnlineClassDatas(startDate, mapBean, studentId, teacherIds);
	}

	public void getHomeworkDatas(LocalDate startDate, HomeworkDashboardRespVO mapBean, String studentId, List<String> teacherIds) {
		//查询教室中的全部老师
		MidoHomeworkMainMapper midoHomeworkMainMapper = SpringContextHolder.getBean(MidoHomeworkMainMapper.class);
		//查询当天作业完成情况
		List<String> statusList = new ArrayList<>();
		//查询当天是否有被提醒
		List<String> dingString = new ArrayList<>();

		statusList.addAll(midoHomeworkMainMapper.getHomeWorkStatusByMulti(startDate, studentId, teacherIds));
		dingString.addAll(midoHomeworkMainMapper.getHomeWorkDingByMulti(startDate, studentId, teacherIds));
		//没有作业
		if (statusList == null || statusList.size() == 0) {
			mapBean.setHomeworkStatus(HomeworkStatusEnum.NO_HOMEWORK.getCode());
			return;
		} else {
			mapBean.setHomeworkCount(statusList.size());
		}
		Boolean dingFlg = false;
		if (dingString.contains("1")) {
			dingFlg = true;
		}

		int allNO = 0;
		int allOk = 0;
		//循环判断状态
		for (String status : statusList) {
			//作业状态(0-未开始,1-未完成,2-已完成)
			if (HomeWorkConstants.NOT_STARTED.equals(status) || HomeWorkConstants.UNFINISHED.equals(status)) {
				allNO++;
			}
			if (HomeWorkConstants.COMPELTED.equals(status)) {
				allOk++;
			}
		}

		BigDecimal countBD = new BigDecimal(allNO + allOk);
		BigDecimal countDoneBD = new BigDecimal(allOk);
		if (BigDecimal.ZERO.compareTo(countBD) != 0) {
			BigDecimal homeworkRateBD = countDoneBD.multiply(new BigDecimal(100)).divide(countBD, 2, RoundingMode.HALF_UP);
			mapBean.setHomeworkRate(homeworkRateBD);
		}
		//全没完成
		if (allNO == statusList.size()) {
			mapBean.setHomeworkStatus(HomeworkStatusEnum.ALL_UNFINISHED.getCode());
		}

		//全没完成并且提醒了
		if (allNO == statusList.size() && dingFlg) {
			mapBean.setHomeworkStatus(HomeworkStatusEnum.SUCCESS.getCode());
		}

		//全部完成
		if (allOk == statusList.size()) {
			mapBean.setHomeworkStatus(HomeworkStatusEnum.ALL_FINISHED.getCode());
		}

		//一半一半
		if (allNO != statusList.size() && allOk != statusList.size()) {
			mapBean.setHomeworkStatus(HomeworkStatusEnum.HALF_FINISHED.getCode());
		}

		//一半一半 并且提醒了
		if (allNO != statusList.size() && allOk != statusList.size() && dingFlg) {
			mapBean.setHomeworkStatus(HomeworkStatusEnum.HALF_FINISHED_REMINDED.getCode());
		}
	}

	public void getOfflineClassDatas(LocalDate startDate, HomeworkDashboardRespVO mapBean, String studentId, List<String> teacherIds) {
		/* 线下课查询 */
		MultiTeacherOfflineClassMapper multiTeacherOfflineClassMapper = SpringContextHolder
				.getBean(MultiTeacherOfflineClassMapper.class);

		LambdaQueryWrapper<MultiTeacherOfflineClass> multiTeacherOfflineClassQW = Wrappers.<MultiTeacherOfflineClass>lambdaQuery()
				.eq(MultiTeacherOfflineClass::getClassData, startDate)
				.eq(MultiTeacherOfflineClass::getStudentId, studentId);
		if (CollectionUtils.isNotEmpty(teacherIds)) {
			multiTeacherOfflineClassQW.in(MultiTeacherOfflineClass::getTeacherId, teacherIds);
		}
		multiTeacherOfflineClassQW.orderByDesc(MultiTeacherOfflineClass::getCreateTime);
		List<MultiTeacherOfflineClass> multiTeacherOfflineClassList = multiTeacherOfflineClassMapper
				.selectList(multiTeacherOfflineClassQW);
		if (CollectionUtils.isNotEmpty(multiTeacherOfflineClassList)) {
			BigDecimal offlineCourseLengthBD = multiTeacherOfflineClassList.stream().map(item -> {
				Integer actualTime = item.getActualTimeLength();
				if (Objects.nonNull(actualTime)) {
					return new BigDecimal(actualTime);
				}
				if (Objects.isNull(item.getEndTime())) {
					return new BigDecimal(item.getSetTimeLength());
				}
				return new BigDecimal(ChronoUnit.MINUTES.between(item.getStartTime(), item.getEndTime()));
			}).reduce(BigDecimal.ZERO, BigDecimal::add);

			double offlineCourseLength = offlineCourseLengthBD.divide(new BigDecimal(60), 1, BigDecimal.ROUND_HALF_UP).doubleValue();
			mapBean.setOfflineCourseLength(offlineCourseLength);
		}
	}

	public void getOnlineClassDatas(LocalDate startDate, HomeworkDashboardRespVO mapBean, String studentId, List<String> teacherIds) {
		/* 云课堂查询 */
		OnlineCourseInfoMapper onlineCourseInfoMapper = SpringContextHolder.getBean(OnlineCourseInfoMapper.class);
		OnlineCoursePeriodMapper onlineCoursePeriodMapper = SpringContextHolder.getBean(OnlineCoursePeriodMapper.class);

		LambdaQueryWrapper<OnlineCourseInfo> onlineCourseInfoQW = Wrappers.<OnlineCourseInfo>lambdaQuery()
				.eq(OnlineCourseInfo::getCourseDate, startDate)
				.eq(OnlineCourseInfo::getStudentId, studentId);
		if (CollectionUtils.isNotEmpty(teacherIds)) {
			onlineCourseInfoQW.in(OnlineCourseInfo::getTeacherId, teacherIds);
		}
		onlineCourseInfoQW.orderByDesc(OnlineCourseInfo::getCreateTime);
		List<OnlineCourseInfo> onlineCourseInfoList = onlineCourseInfoMapper.selectList(onlineCourseInfoQW);
		if (CollectionUtils.isNotEmpty(onlineCourseInfoList)) {
			List<Long> courseInfoIdList = onlineCourseInfoList.stream().map(OnlineCourseInfo::getId).collect(Collectors.toList());
			LambdaQueryWrapper<OnlineCoursePeriod> onlineCoursePeriodQW = Wrappers.<OnlineCoursePeriod>lambdaQuery()
					.in(OnlineCoursePeriod::getCourseId, courseInfoIdList);
			List<OnlineCoursePeriod> onlineCoursePeriodList = onlineCoursePeriodMapper.selectList(onlineCoursePeriodQW);
			if (CollectionUtils.isNotEmpty(onlineCoursePeriodList)) {
				BigDecimal onlineCourseLengthBD = onlineCoursePeriodList.stream().map(item -> {
					return new BigDecimal(ChronoUnit.SECONDS.between(item.getStartTime(), item.getEndTime()));
				}).reduce(BigDecimal.ZERO, BigDecimal::add);

				double onlineCourseLength = onlineCourseLengthBD.divide(new BigDecimal(60), 1, BigDecimal.ROUND_HALF_UP).doubleValue();

				mapBean.setOnlineCourseLength(onlineCourseLength);
			}
		}
	}

}